package com.gdk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.CategoryService;
import com.gdk.service.GoodsService;
import com.gdk.service.InterestService;
import com.gdk.service.LoginService;
import com.gdk.service.QyService;
import com.gdk.service.ServiceService;
import com.gdk.service.TreeService;
import com.gdk.utils.XVDateUtils;


/**
 * 商品controller
 * */

@Controller
public class GoodsController {
	
	@Autowired
    private GoodsService goodsService;
	@Autowired
    private CategoryService categoryService;
	@Autowired
    private ServiceService serviceService;
	@Autowired
    private InterestService interestService;
	@Autowired
    private TreeService treeService;
	@Autowired
    private QyService qyService;
	/**
	 * 添加前查询出所有分类
	 * */
	@RequestMapping("/preAddGoods.do")
	public String preAddGoods(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		List<Map<String, Object>> allCategory = categoryService.selectAllCategory();
	
		request.setAttribute("allCategory", allCategory);
		
		//查询出所有客服
		List<Map<String, Object>> allService = serviceService.selectAllService();
		request.setAttribute("allService", allService);
		
		//查询出所有权益
		List<Map<String, Object>> allInterest = interestService.selectAllInterest();
		request.setAttribute("allInterest", allInterest);
		return "jsp/goods/goods-add";
	}

	/**
	 * 新增商品
	 * */
	@ResponseBody
	@RequestMapping("/addGoods.do")
	public int addGoods(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("rg_status", 1);
		map.put("rg_ctime", XVDateUtils.getCurrenTimeMillis());
		System.out.println("商品插入  :  "+map);
		
		Map<String, Object> cutMillis = XVDateUtils.dateCutMillis(map.get("pay_date").toString());
		map.put("rg_atime", cutMillis.get("f_time"));
		map.put("rg_etime", cutMillis.get("e_time"));
		int re_status = goodsService.addGoods(map);
		System.out.println("结果 : "+re_status);
		if(re_status == 1) {
			//添加快照
			map.put("new_updatetime", XVDateUtils.getCurrenTimeMillis());
			goodsService.addGoodsPhoto(map);
		}
		return re_status;
	}
	
	/**
	 * 查询所有商品
	 * */
	@RequestMapping("/selectAllGoods.do")
	public String selectAllGoods(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allGoods = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllGoods(map).size();
		
		map.put("allGoodsItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allGoods = goodsService.selectAllGoods(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allGoods) {
			if(map2.get("rg_ctime") != null) {
				int p = Integer.valueOf(map2.get("rg_ctime").toString());
				if(p != 0) {
					map2.put("rg_ctime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("rg_atime") != null) {
				int p = Integer.valueOf(map2.get("rg_atime").toString());
				if(p != 0) {
					map2.put("rg_atime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("rg_etime") != null) {
				int p = Integer.valueOf(map2.get("rg_etime").toString());
				if(p != 0) {
					map2.put("rg_etime", XVDateUtils.MillisFormat(p));	
				}
			}
			
			
			
		}
		
		
		request.setAttribute("allGoods",allGoods);
		return "jsp/goods/goods-list";
	}
	
	/**
	 *修改前查询商品详情
	 * */
	@RequestMapping("/preSelectGoodsDetails.do")
	public String preSelectGoodsDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		List<Map<String, Object>> allCategory = categoryService.selectAllCategory();
		
		request.setAttribute("allCategory", allCategory);
		Map<String, Object> goodsDetails = goodsService.selectGoodsDetails(map);
		
		request.setAttribute("goodsDetails",goodsDetails);
		
		//查询出所有客服
		List<Map<String, Object>> allService = serviceService.selectAllService();
		request.setAttribute("allService", allService);
		//查询出所有权益
		List<Map<String, Object>> allInterest = interestService.selectAllInterest();
		request.setAttribute("allInterest", allInterest);
		
		return "jsp/goods/goods-edit";
	}
	
	/**
	 * 修改商品
	 * */
	@ResponseBody
	@RequestMapping("/updateGoods.do")
	public int updateGoods(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("商品 : "+map);
		
		
		
		if(map.get("pay_date") !=null && !"".equals(map.get("pay_date").toString())) {
			System.out.println("走了");
			Map<String, Object> cutMillis = XVDateUtils.dateCutMillis(map.get("pay_date").toString());
			map.put("rg_atime", cutMillis.get("f_time"));
			map.put("rg_etime", cutMillis.get("e_time"));
			
		}
		
		int shop = goodsService.updateGoodsDetails(map);
		if(shop == 1) {
			//查询是否只修改状态
			if(map.get("rg_status") !=null && !"".equals(map.get("rg_status").toString())) {
				Map<String, Object> goodsDetails = goodsService.selectGoodsDetails(map);
				
				goodsDetails.put("rg_status", map.get("rg_status"));
				
				if(map.get("rg_atime") !=null && !"".equals(map.get("rg_atime").toString())) {
					goodsDetails.put("rg_atime", XVDateUtils.stringDateToMillis(goodsDetails.get("rg_atime").toString()));
					goodsDetails.put("rg_etime", XVDateUtils.stringDateToMillis(goodsDetails.get("rg_etime").toString()));
					
				}
				
				
				map = goodsDetails;
			}
			
			if(map.get("rg_atime") !=null && !"".equals(map.get("rg_atime").toString())) {
				map.put("rg_atime", XVDateUtils.stringDateToMillis(map.get("rg_atime").toString()));
				map.put("rg_etime", XVDateUtils.stringDateToMillis(map.get("rg_etime").toString()));
				
			}
			//添加快照
			map.put("new_updatetime", XVDateUtils.getCurrenTimeMillis());
			goodsService.addGoodsPhoto(map);
		}
		return shop;
	}
	/**
	 * 	查询所有产值数据
	 * */
	@RequestMapping("/selectAllReceipt.do")
	public String selectAllReceipt(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allReceipt = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllReceipt(map).size();
		
		map.put("allReceiptItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-receipt-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allReceipt = goodsService.selectAllReceipt(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allReceipt) {
			if(map2.get("pay_time") != null) {
				int p = Integer.valueOf(map2.get("pay_time").toString());
				if(p != 0) {
					map2.put("pay_time", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("rr_time") != null) {
				int p = Integer.valueOf(map2.get("rr_time").toString());
				if(p != 0) {
					map2.put("rr_time", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("rr_ftime") != null) {
				int p = Integer.valueOf(map2.get("rr_ftime").toString());
				if(p != 0) {
					map2.put("rr_ftime", XVDateUtils.MillisFormat(p));	
				}
			}
			
			
			
		}
		
		
		request.setAttribute("allReceipt",allReceipt);
		return "jsp/goods/goods-receipt-list";
	}
	
	/**
	 * 修改产值信息
	 * */
	@ResponseBody
	@RequestMapping("/updateReceipt.do")
	public int updateReceipt(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		int i = goodsService.updateReceiptDetails(map);
		return i;
	}
	
	
	/**
	 * 查询所有代售申请
	 * */
	@RequestMapping("/selectAllDs.do")
	public String selectAllDs(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allDs = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllDs(map).size();
		
		map.put("allDsItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-ds-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allDs = goodsService.selectAllDs(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allDs) {
			if(map2.get("ors_time") != null) {
				int p = Integer.valueOf(map2.get("ors_time").toString());
				if(p != 0) {
					map2.put("ors_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allDs",allDs);
		return "jsp/goods/goods-ds-list";
	}
	
	/**
	 *   新增收获前查询
	 * */
	@RequestMapping("/preAddShouhuo.do")
	public String preAddShouhuo(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		//查询出所有未过期的树
		//给指定的树增加产量
		System.out.println("查询  :  "+map);

		map.put("now_time",XVDateUtils.getCurrenTimeMillis());
		
		
		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allTree = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllDs(map).size();
		
		map.put("allTreeItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-tree-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allTree = goodsService.selectAllTreeAndUser(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allTree) {
			if(map2.get("or_end_time") != null) {
				int p = Integer.valueOf(map2.get("or_end_time").toString());
				if(p != 0) {
					map2.put("or_end_time", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("or_c_time") != null) {
				int p = Integer.valueOf(map2.get("or_c_time").toString());
				if(p != 0) {
					map2.put("or_c_time", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("ft_time") != null) {
				int p = Integer.valueOf(map2.get("ft_time").toString());
				if(p != 0) {
					map2.put("ft_time", XVDateUtils.MillisFormat(p));	
				}
			}
		}
		
		
		request.setAttribute("allTree",allTree);
		return "jsp/goods/goods-tree-list";
		
	}
	
	/**
	 *   新增收获
	 * */
	@RequestMapping("/addShouhuo.do")
	@ResponseBody
	public int addShouhuo(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("fors_time", XVDateUtils.getCurrenTimeMillis());
		map.put("fors_status", 0);
		int fors_count = Integer.valueOf(map.get("fors_count").toString());
		
		int o_flag = Integer.valueOf(map.get("o_flag").toString());
		Map<String,Object> flagmap = new HashMap<String,Object>();
		
		
		Map<String,Object> tmap = new HashMap<String,Object>();
		tmap.put("or_id", map.get("or_id"));
	
		flagmap.put("or_id", map.get("or_id"));
		flagmap.put("u_id", map.get("u_id"));
		flagmap.put("fors_status", 1);
		flagmap.put("fors_text", "特殊商品直接出库");
		flagmap.put("fors_count", fors_count);
		
		
		//查询树的产量
		List<Map<String, Object>> allTreeShouhuoByTree = goodsService.selectAllTreeShouhuo(tmap);
		if(allTreeShouhuoByTree.size() > 0) {
			Map<String,Object> t = allTreeShouhuoByTree.get(0);
			int fors_sumcount = Integer.valueOf(t.get("fors_sumcount").toString());
			int fors_now_count = Integer.valueOf(t.get("fors_now_count").toString());
			
			map.put("fors_sumcount", fors_sumcount+fors_count);
			map.put("fors_now_count", fors_now_count+fors_count);
			
			flagmap.put("fors_sumcount", fors_sumcount+fors_count);
			flagmap.put("fors_now_count", fors_now_count);
			
		}else {
			map.put("fors_sumcount", fors_count);
			map.put("fors_now_count",fors_count);
			
			flagmap.put("fors_sumcount", fors_count);
			flagmap.put("fors_now_count", fors_count);
			
		}
		
		
		//查询用户的产量
		Map<String,Object> umap = new HashMap<String,Object>();
		umap.put("u_id", map.get("u_id"));
	
		//查询树的产量
		List<Map<String, Object>> allTreeShouhuoByUser = goodsService.selectAllTreeShouhuo(umap);
		if(allTreeShouhuoByUser.size() > 0) {
			Map<String,Object> t = allTreeShouhuoByUser.get(0);
			int fors_u_newcount = Integer.valueOf(t.get("fors_u_newcount").toString());
			int fors_u_sumcount = Integer.valueOf(t.get("fors_u_sumcount").toString());
			
			map.put("fors_u_newcount", fors_u_newcount+fors_count);
			map.put("fors_u_sumcount", fors_u_sumcount+fors_count);
			
			flagmap.put("fors_u_newcount", fors_u_newcount);
			flagmap.put("fors_u_sumcount", fors_u_sumcount+fors_count);
			
		}else {
			map.put("fors_u_newcount", fors_count);
			map.put("fors_u_sumcount",fors_count);
			
			flagmap.put("fors_u_newcount", fors_count);
			flagmap.put("fors_u_sumcount", fors_count);
		}
		
		
		System.out.println("收获Map : "+map);
		int i = goodsService.addTreeReceiptAndUser(map);
		
		/*如果为直接入库的树*/
		
		/*查询出此用户最新总量*/
		if(o_flag == 1) {
			//从收获表出库
			flagmap.put("fors_time", XVDateUtils.getCurrenTimeMillis());
			goodsService.addTreeReceiptAndUser(flagmap);
			
			Map<String,Object> dsmap = new HashMap<String,Object>();
			dsmap.put("u_id", map.get("u_id"));
			
			List<Map<String, Object>> allApplyDaishou = goodsService.selectAllApplyDaishou(dsmap);
			
			if(allApplyDaishou.size() > 0) {
				Map<String, Object> map2 = allApplyDaishou.get(0);
				int ds_sumcount = Integer.valueOf(map2.get("ds_sumcount").toString());
				dsmap.put("ds_sumcount", ds_sumcount+fors_count);
			}else {
				dsmap.put("ds_sumcount",fors_count);
			}
			
			//入代售表
			dsmap.put("or_id", map.get("or_id"));
			dsmap.put("ds_status", 1);
			dsmap.put("fors_text", "特殊商品直接出库");
			dsmap.put("ds_count",fors_count);
			dsmap.put("ds_type", 0);
			dsmap.put("ds_time", XVDateUtils.getCurrenTimeMillis());
			dsmap.put("ds_price", map.get("ds_price"));
			
			i = goodsService.addTreeGoodsDaishou(dsmap);
			
		}
		
		
		
		
		return i;
	}
	/**
	 * 查询所有收获记录
	 * */
	@RequestMapping("/selectAllShouhuoLogByTree.do")
	public String selectAllShouhuoLogByTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		List<Map<String, Object>> allTreeShouhuo = goodsService.selectAllTreeShouhuo(map);
		
		for (Map<String, Object> map2 : allTreeShouhuo) {
			if(map2.get("fors_time") != null) {
				int p = Integer.valueOf(map2.get("fors_time").toString());
				if(p != 0) {
					map2.put("fors_time", XVDateUtils.MillisFormat(p));	
				}
			}
		}
		
		request.setAttribute("allTreeShouhuo",allTreeShouhuo);
		return "jsp/goods/goods-tree-shouhuo-log";	
	}
	
	/**
	 * 新增操作纪律
	 * */
	@ResponseBody
	@RequestMapping("/addTreeHandle.do")
	public int addTreeHandle(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		map.put("rr_time",XVDateUtils.stringDateToMillis(map.get("rr_time").toString()));
		int i = goodsService.addTreeHandle(map);
		return i;
	}
	
	/**
	 * 查询所有操作记录
	 * */
	@RequestMapping("/selectAllHandleByTree.do")
	public String selectAllHandleByTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		List<Map<String, Object>> allTreeHandle = goodsService.selectAllHandleByTree(map);
		
		for (Map<String, Object> map2 : allTreeHandle) {
			if(map2.get("rr_time") != null) {
				int p = Integer.valueOf(map2.get("rr_time").toString());
				if(p != 0) {
					map2.put("rr_time", XVDateUtils.MillisFormat(p));	
				}
			}
		}
		
		request.setAttribute("allTreeHandle",allTreeHandle);
		return "jsp/goods/goods-tree-handle-log";	
	}
	
	/**
	 * 查询所有商品代售申请
	 * */
	@RequestMapping("/selectAllApplyDaishou.do")
	public String selectAllApplyDaishou(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allApplyDaishou = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllApplyDaishou(map).size();
		
		map.put("allApplyDaishouItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-apply-daishou-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allApplyDaishou = goodsService.selectAllApplyDaishou(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allApplyDaishou) {
			if(map2.get("ds_time") != null) {
				int p = Integer.valueOf(map2.get("ds_time").toString());
				if(p != 0) {
					map2.put("ds_time", XVDateUtils.MillisFormat(p));	
				}
			}
		
			
			
		}
		
		
		request.setAttribute("allApplyDaishou",allApplyDaishou);
		return "jsp/goods/goods-apply-daishou-list";
	}
	/**
	 * 修改申请状态
	 * */
	@ResponseBody
	@RequestMapping("/updateApplyDaishouStatus.do")
	public int updateApplyDaishouStatus(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		//修改代售表状态		
		int applyDaishouStatus = goodsService.updateApplyDaishouStatus(map);
		
		int goods =0;
		if(applyDaishouStatus == 1) {
			//商品表新增代售商品
			Map<String,Object> goodsMap = new HashMap<String,Object>();
			goodsMap.put("ds_id", map.get("ds_id"));
			goodsMap.put("rc_id", 3);
			goodsMap.put("rg_name", "阳光玫瑰葡萄");
			goodsMap.put("rg_shortname", "阳光玫瑰葡萄");
			//goodsMap.put("rg_content", "阳光玫瑰葡萄");
			goodsMap.put("rg_until","斤");
			goodsMap.put("rg_specifications", "{\"默认\":\"10\"}");
			
			goodsMap.put("rg_sold", 0);
			goodsMap.put("rg_status", 1);
			goodsMap.put("rg_ctime", XVDateUtils.getCurrenTimeMillis());
			goodsMap.put("rg_atime", XVDateUtils.getCurrenTimeMillis());
			goodsMap.put("rg_etime", XVDateUtils.getCurrenTimeMillis()+15811200);//加半年有效期
			goodsMap.put("rg_goods_type", 3);
			
			goods = goodsService.addGoods(goodsMap);
			
			if(goods == 1) {
				goodsMap.put("new_updatetime", XVDateUtils.getCurrenTimeMillis());
				int addGoodsPhoto = goodsService.addGoodsPhoto(goodsMap);
			}
			
		}
		
		
		
		
		return goods;
	}
	
	
	
	/**
	 * 查询所有商品保鲜
	 * */
	@RequestMapping("/selectAllBaoxian.do")
	public String selectAllBaoxian(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allBaoxian = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllBaoxian(map).size();
		
		map.put("allBaoxianItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-baoxian-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allBaoxian = goodsService.selectAllBaoxian(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allBaoxian) {
			if(map2.get("forb_addtime") != null) {
				int p = Integer.valueOf(map2.get("forb_addtime").toString());
				if(p != 0) {
					map2.put("forb_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("forb_paytime") != null) {
				int p = Integer.valueOf(map2.get("forb_paytime").toString());
				if(p != 0) {
					map2.put("forb_paytime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("forb_endtime") != null) {
				int p = Integer.valueOf(map2.get("forb_endtime").toString());
				if(p != 0) {
					map2.put("forb_endtime", XVDateUtils.MillisFormat(p));	
				}
			}
			
			
		}
		
		
		request.setAttribute("allBaoxian",allBaoxian);
		return "jsp/goods/goods-baoxian-list";
	}
	
	/**
	 * 修改认养树的申请到期时间
	 * */
	@ResponseBody
	@RequestMapping("/updateUserAndTreeDetails.do")
	public int updateUserAndTreeDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("or_end_time", XVDateUtils.getCurrenTimeMillis()-10);
		
		int i = goodsService.updateUserAndTreeDetails(map);
		map.put("ft_endtime", XVDateUtils.getCurrenTimeMillis()-10);
		treeService.updateTreeDetails(map);
		return i;
	}
	
	/**
	 * 修改保鲜详情
	 * */
	@ResponseBody
	@RequestMapping("/updateBaoxianDeatils.do")
	public int updateBaoxianDeatils(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		int i = goodsService.updateBaoxianDetails(map);
		
		return i;
	}
	

	/**
	 * 查询所有商品发货信息
	 * */
	@RequestMapping("/selectAllFahuo.do")
	public String selectAllFahuo(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allFahuo = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllFahuo(map).size();
		
		map.put("allFahuoItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/goods/goods-fahuo-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allFahuo = goodsService.selectAllFahuo(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allFahuo) {
			if(map2.get("fh_addtime") != null) {
				int p = Integer.valueOf(map2.get("fh_addtime").toString());
				if(p != 0) {
					map2.put("fh_addtime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("fh_ftime") != null) {
				int p = Integer.valueOf(map2.get("fh_ftime").toString());
				if(p != 0) {
					map2.put("fh_ftime", XVDateUtils.MillisFormat(p));	
				}
			}
			if(map2.get("fh_paytime") != null) {
				int p = Integer.valueOf(map2.get("fh_paytime").toString());
				if(p != 0) {
					map2.put("fh_paytime", XVDateUtils.MillisFormat(p));	
				}
			}
			
			
		}
		
		
		request.setAttribute("allFahuo",allFahuo);
		return "jsp/goods/goods-fahuo-list";
	}
	
	
	/**
	 * 修改发货详情
	 * */
	@ResponseBody
	@RequestMapping("/fahuoTree.do")
	public int fahuoTree(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		map.put("fh_ftime",XVDateUtils.getCurrenTimeMillis());
		int i = goodsService.updateFahuoDetails(map);
		
		return i;
	}
	/**
	 * 新增发货信息前查询
	 * */
	@RequestMapping("/preAddExpressFahuo.do")
	public String preAddExpressFahuo(HttpServletRequest request) {
		//发货前查询出所有未到期被认养的树
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("now_time", XVDateUtils.getCurrenTimeMillis());
		List<Map<String, Object>> allTreeAndUser = goodsService.selectAllTreeAndUser(map);
		request.setAttribute("allTreeAndUser", allTreeAndUser);
		
		//查询出此树的权益
		
		//查询出此树的库存
		
		
		
		return "jsp/goods/goods-baoxian-express-add";	
	}
	
	/**
	 * or_id查询某棵树的保鲜
	 * */
	@ResponseBody
	@RequestMapping("/selectBaoxianByTreeUser.do")
	public Map<String,Object> selectBaoxianByTreeUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		Map<String,Object> re = null;
		List<Map<String,Object>> rel = goodsService.selectAllTreeShouhuo(map);
		if(rel.size() > 0) {
			re = rel.get(0);
		}
		
		return re;
	}
	/**
	 * or_id查询某棵树的保鲜
	 * */
	@ResponseBody
	@RequestMapping("/selectQyByTreeUser.do")
	public Map<String,Object> selectQyByTreeUser(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		Map<String,Object> rel = goodsService.selectRecognizeTreeByTree(map);
		
		map.put("u_id", rel.get("u_id"));
		map.put("fa_id",5);
		
		Map<String, Object> qy = goodsService.selectNowQyByQy(map);
		return qy;
	}
	/**
	 * 新增发货信息
	 * */
	@ResponseBody
	@RequestMapping("/addExpressFahuo.do")
	public int addExpressFahuo(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("发货map : "+map);
		//新增收货产值,减少
		//addTreeReceiptAndUser
		//查询所有收获 
		//selectAllTreeShouhuo
		
		
		Map<String,Object> czMap = new HashMap<String,Object>();
		
		czMap.put("fors_time", XVDateUtils.getCurrenTimeMillis());
		czMap.put("fors_status", 1);
		
		czMap.put("or_id", map.get("or_id"));
		czMap.put("u_id", map.get("u_id"));
		czMap.put("fors_text","后台操作发货");
		czMap.put("fors_count", map.get("fh_num"));
		int fors_count = Integer.valueOf(map.get("fh_num").toString());
		
		Map<String,Object> tmap = new HashMap<String,Object>();
		tmap.put("or_id", map.get("or_id"));
	
		//查询树的产量
		List<Map<String, Object>> allTreeShouhuoByTree = goodsService.selectAllTreeShouhuo(tmap);
		if(allTreeShouhuoByTree.size() > 0) {
			Map<String,Object> t = allTreeShouhuoByTree.get(0);
			int fors_sumcount = Integer.valueOf(t.get("fors_sumcount").toString());
			int fors_now_count = Integer.valueOf(t.get("fors_now_count").toString());
			
			//czMap.put("fors_sumcount", fors_sumcount-fors_count);
			czMap.put("fors_now_count", fors_now_count-fors_count);
			
		}else {
			czMap.put("fors_sumcount", 0);
			czMap.put("fors_now_count",0);
		}
		
		
		//查询用户的产量
		Map<String,Object> umap = new HashMap<String,Object>();
		umap.put("u_id", map.get("u_id"));
	
		//查询树的产量
		List<Map<String, Object>> allTreeShouhuoByUser = goodsService.selectAllTreeShouhuo(umap);
		if(allTreeShouhuoByUser.size() > 0) {
			Map<String,Object> t = allTreeShouhuoByUser.get(0);
			int fors_u_newcount = Integer.valueOf(t.get("fors_u_newcount").toString());
			int fors_u_sumcount = Integer.valueOf(t.get("fors_u_sumcount").toString());
			
			czMap.put("fors_u_newcount", fors_u_newcount-fors_count);
			//czMap.put("fors_u_sumcount", fors_u_sumcount-fors_count);
			
		}else {
			czMap.put("fors_u_newcount", 0);
			czMap.put("fors_u_sumcount",0);
		}
		
		
		System.out.println("发货收获Map : "+czMap);
		int k =0;
		 int i = goodsService.addTreeReceiptAndUser(czMap);
		if( i ==1) {			
			//新增权益使用记录
			Map<String,Object> qyMap = new HashMap<String,Object>();
			
			qyMap.put("qy_time", XVDateUtils.getCurrenTimeMillis());
			qyMap.put("o_id", 0);
			qyMap.put("qy_sumcount", map.get("qy_sumcount"));
			qyMap.put("or_id", map.get("or_id"));
			qyMap.put("u_id", map.get("u_id"));
			qyMap.put("fa_id",5);
			qyMap.put("qy_type",1);
			qyMap.put("qy_count",1);
			
			int j = qyService.bindQy(qyMap);
			if(j == 1) {
				//新增发货记录
				Map<String,Object> fhMap = new HashMap<String,Object>();
				
				
				fhMap.put("fh_name", map.get("fh_name"));
				fhMap.put("fh_num", map.get("fh_num"));
				fhMap.put("fh_phone", map.get("fh_phone"));
				
				fhMap.put("fh_address", map.get("fh_address"));
				fhMap.put("or_id", map.get("or_id"));
				fhMap.put("u_id", map.get("u_id"));
				fhMap.put("fh_wl", map.get("fh_wl"));
				fhMap.put("fh_wl_num", map.get("fh_wl_num"));
				fhMap.put("fh_img", map.get("fh_img"));
				
				fhMap.put("fh_addtime", XVDateUtils.getCurrenTimeMillis());
				fhMap.put("fh_ftime", XVDateUtils.getCurrenTimeMillis());
				
				fhMap.put("fh_desc", map.get("fh_desc"));
				fhMap.put("fh_type",1);
				fhMap.put("fh_status",1);
				
				
				k  = goodsService.addFahuoDetails(fhMap);
				
			}
			
		}
		
		return k;
	}
	
	/**
	 * 代售前查询出最新用户代售信息
	 * */
	@RequestMapping("/preAddTreeGoodsDaishouOrder.do")
	public String preAddTreeGoodsDaishouOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		List<Map<String, Object>> allDaishou = goodsService.selectAllApplyDaishouByUser();
		request.setAttribute("allDaishou", allDaishou);
		return "jsp/ds/ds-order-add";
	}
	
	
	//selectAllApplyDaishou
	
	/**
	 * 新增代售订单
	 * 
	 * */
	@ResponseBody
	@RequestMapping("/addTreeGoodsDaishouOrder.do")
	public int addTreeGoodsDaishouOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("map : "+map);
		double uo_summoney = 0;
		double uo_newmoney = 0;
		double uo_money = Double.valueOf(map.get("uo_money").toString());
		//查询前
		List<Map<String, Object>> allDaishouOrderByUser = goodsService.selectAllDaishouOrderByUser(map);
		if(allDaishouOrderByUser.size() > 0) {
			uo_summoney = Double.valueOf(allDaishouOrderByUser.get(0).get("uo_summoney").toString());
			uo_newmoney = Double.valueOf(allDaishouOrderByUser.get(0).get("uo_newmoney").toString());
		}
		
		//新增代售发货信息
		map.put("uo_time", XVDateUtils.getCurrenTimeMillis());
		map.put("uo_type", 1);
		map.put("uo_summoney", uo_summoney+uo_money);
		map.put("uo_newmoney", uo_newmoney+uo_money);
		
		int i = goodsService.addTreeGoodsDaishouOrder(map);
		if(i == 1) {
			//减少代售表库存
			Map<String,Object> kmap = new HashMap<String,Object>();
			kmap.put("u_id", map.get("u_id"));
			//查询最新数据
			Map<String, Object> dsmap = goodsService.selectAllApplyDaishou(kmap).get(0);
			int uo_count = Integer.valueOf(map.get("uo_count").toString());
			kmap.put("ds_count", uo_count);
			kmap.put("ds_status", 1);
			kmap.put("ds_time", XVDateUtils.getCurrenTimeMillis());
			kmap.put("ds_type", 1);
			int ds_sumcount = Integer.valueOf(dsmap.get("ds_sumcount").toString());
			kmap.put("ds_sumcount", ds_sumcount-uo_count);
			
			goodsService.addTreeGoodsDaishou(kmap);
			
		}
		
		
		
		return i;
	}
	
	/**
	 * 查询所有代售订单
	 * */
	@RequestMapping("/selectAllDaishouOrder.do")
	public String selectAllDaishouOrder(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		int page_num = 20;
		int number = 1;
		int size = 0;
		List<Map<String, Object>> allDs = null;
	
	
		if(!("".equals(map.get("page_num")) || map.get("page_num") == null)) {
			page_num = Integer.parseInt(map.get("page_num").toString());
		}
		
		map.put("page_num", page_num);
	//	System.out.println("所有订单查询"+map);
		if(map.get("number") != null && !"".equals(map.get("number")) ) {
			number = Integer.valueOf(map.get("number").toString());
		}
		int pageNum = number;
	//	System.out.println("pageNum  :  "+pageNum);
		map.remove("number");
		//map.put("number", number);
	//	System.out.println("map  : "+map);
		
		String nnnm = page_num+"";
	//	System.out.println("nnnm  :"+nnnm);
		double page_float = Double.parseDouble(nnnm);
		
		
		int itemCount = goodsService.selectAllDaishouOrder(map).size();
		
		map.put("allDsItem",itemCount);
		
		
		
		size = (int) Math.ceil(itemCount/page_float);
		//size = (int) Math.ceil(orderService.doSelectAllOrder(map).size()/10.0f);
		//System.out.println("分页数量  : "+size);
		if (size == 0) {
	
			request.setAttribute("msg", "没符合的数据");
			return "jsp/ds/ds-order-list";
		}
		if (number == 1) {
			map.put("number", 0);
		} else {
			map.put("number",(number-1)*page_num);
		}
//						System.err.println("分页数据    :  "+map);

		//分页所有订单
		allDs = goodsService.selectAllDaishouOrder(map);

		
		//所有类型
		map.put("number", pageNum);
		map.put("sumNumber", size);
		//model.addAttribute("page", map);
		request.setAttribute("page",map);
		System.out.println("page_map map内含  :   "+map);
		
		//List<Map<String, Object>> supplyHXCode = orderService.selectSupplyHXCode(map);
	
		for (Map<String, Object> map2 : allDs) {
			if(map2.get("uo_time") != null) {
				int p = Integer.valueOf(map2.get("uo_time").toString());
				if(p != 0) {
					map2.put("uo_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allDs",allDs);
		return "jsp/ds/ds-order-list";
	}
	
}